package dao;

import pojo.Room;
import pojo.Room;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class RoomDaoImpl implements RoomDao{
    @Override
    public ArrayList<Room> findAll() {
        ArrayList<Room> list = new ArrayList<Room>();
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM dorm ";
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while(rs.next()){
                Room room = new Room(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getBigDecimal(4),rs.getInt(5),rs.getBigDecimal(6));
                list.add(room);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con,statement,rs);
        }
        return list;
    }

    @Override
    public Room findById(Integer dormId) {
        Room room = null;
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM dorm WHERE dormID = ?";
            statement = con.prepareStatement(sql);
            statement.setInt(1,dormId);
            rs = statement.executeQuery();
            while(rs.next()){
                room = new Room();
                room.setRoomId(rs.getInt(1));
                room.setSex(rs.getString(2));
                room.setSize(rs.getInt(3));
                room.setPrice(rs.getBigDecimal(4));
                room.setEmpty(rs.getInt(5));
                room.setCash(rs.getBigDecimal(6));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con,statement,rs);
        }
        return room;
    }

    @Override
    public int insert(Room room) {
        Connection con = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "INSERT INTO dorm VALUES (?,?,?,?,?,?) ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,room.getRoomId());
            statement.setString(2,room.getSex());
            statement.setInt(3,room.getSize());
            statement.setBigDecimal(4,room.getPrice());
            statement.setInt(5,room.getEmpty());
            statement.setBigDecimal(6,room.getCash());
            result = statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con,statement);
        }
        return result;
    }

    @Override
    public int update(Room room) {
        Connection con = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            con = JDBCUtils.getConnection();
            String sql = "UPDATE dorm SET dormSex = ?,dormSize = ? , dormPrice = ?, dormEmpty = ? ,dormCash = ? WHERE dormID = ?";
            statement = con.prepareStatement(sql);
            statement.setString(1,room.getSex());
            statement.setInt(2,room.getSize());
            statement.setBigDecimal(3,room.getPrice());
            statement.setInt(4,room.getEmpty());
            statement.setBigDecimal(5,room.getCash());
            statement.setInt(6,room.getRoomId());
            result = statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(con,statement);
        }
        return result;
    }

    @Override
    public int delete(Connection con,Room room) throws SQLException {
        PreparedStatement statement = null;
        int result = 0;
        try {
            String sql = "DELETE FROM dorm  WHERE dormID = ?  ";
            statement = con.prepareStatement(sql);
            statement.setInt(1,room.getRoomId());
            result = statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }finally {
            JDBCUtils.close(null,statement);
        }
        return result;
    }
}
